#ifndef __BSW_LPSPI_H__
#define __BSW_LPSPI_H__

#include "lpspi_hw_access.h"

//SPI_CLK=PCS/(2^prediv*(SCKDIV+2))

#if LPSPI_DEBUG 															     /*此处代码为配置举例*/
	lpspi_tx_cmd_config_t Spi_TxCmdCfgSet =
	{
		.whichPcs      = LPSPI_PCS3,									 /* PCS=3:                                                      */
		.width         = LPSPI_SINGLE_BIT_XFER,				 /* WIDTH=0: Single bit transfer                                */
		.clkPhase      = LPSPI_CLOCK_PHASE_1ST_EDGE,	 /* CPHA=0: Change data on SCK lead'g, capture on trail'g edge  */
		.clkPolarity   = LPSPI_SCK_ACTIVE_HIGH,			   /* CPOL=0: SCK inactive state is HIGH                          */
		.lsbFirst      = false,												 /* LSBF=0: Data is transfered MSB first                        */
		.txMask 	     = false,												 /* TXMSK=0: Normal transfer: data loaded from tx FIFO          */                          
		.rxMask		     = false,												 /* RXMSK=0: Normal transfer: rx data stored in rx FIFO         */
		.contTransfer  = true,											   /* CONT, CONTC=0: Continuous transfer disabled                 */
		.contCmd       = false,												 /* Master option to change cmd word within cont transfer.      */
		.frameSize     = 8,														 /* FRAMESZ=7: # bits in frame = 7+1=8                          */
		.preDiv        = 2,														 /* PRESCALE=2: Functional clock divided by 2*2 = 4             */
		.byteSwap      = false, 											 /* BYSW=0: Byte swap disabled                                  */
	};
#endif
	
	
typedef struct
{
	LPSPI_Type* 							 base;						     /* SPI硬件模块选择:  LPSPI0                                    */
	clock_names_t 						 clockName;			       /* SPI的PCC时钟选择: LPSPI0_CLK                                */
	uint32_t 									 clockSource;		       /* PCC时钟选择:      6                                         */
	
	bool											 b_TxDmaCmd;			     /* TXDMA not used                                              */
	bool											 b_RxDmaCmd;			     /* RXDMA not used                                              */
	lpspi_master_slave_mode_t  Lpspi_mode;			     /* Set for master mode                                         */ 
	
	lpspi_pin_config_t 				 PinCfg;					     /* SPI通信数据口设置 SDI-input,SDO-output                      */
	lpspi_data_out_config_t 	 DataOutCfg;			     /* 通信数据口保留状态选择:                                     */
	bool 											 b_Pcs_Enable;		     /* 硬件CS设置: true 使能硬件CS                                 */
	
	bool 											 b_SampleDly;		       /* 延迟采样配置: true 开启延迟采样                             */
	uint32_t 									 txWater;				       /* 设置RxFIFO                                                  */
	uint32_t 									 rxWater;				       /* 设置TxFIFO                                                  */
	lpspi_tx_cmd_config_t* 		 Cmd_Cfg;				       /* 设置SPI传输帧格式                                           */
	uint32_t									 u32_SCKDIV;			     /* 设置波特率: SCKDIV=12  (56/4)/(SCKDIV+2)= 1MHz              */
	uint32_t									 u32_SCKPCS;			     /* 设置SCK-PCS延时 SCKPCS=4   4+1 = 5 (500 nsec)               */
	uint32_t									 u32_PCSSCK;			     /* 设置PCS-SCK延时 PCSSCK=9   9+1 = 10 (1 usec)                */
	uint32_t									 u32_DBT;				       /* 设置传输间隔延时 DBT=8 8+2 = 10 (1 usec)                    */
	lpspi_which_pcs_t 				 Lpspi_Pcs;			       /* 选择CS硬件通道 LPSPI_PCS3                                   */
  lpspi_signal_polarity_t 	 PcsPolarity;		       /* 设置CS片选信号的有效电平 LPSPI_ACTIVE_LOW 低电平有效        */
	
}Bsw_Lpspi_Cfg;



/********************************************************
**函数名：	BSW_LPSPI_INIT
**功能：		初始化 LPSPI_INIT
**输入参数	Lpsi_Cfg	SPI配置结构体
**返回值：	无
**注意：		根据相应的电路配置SPI
*********************************************************/
void BSW_LPSPI_INIT(Bsw_Lpspi_Cfg Lpsi_Cfg);


void BSW_LPSPI_START(LPSPI_Type* base,u16 u16TxData);
u16 BSW_LPSPI_WR(LPSPI_Type * base,u16 u16TxData);

u16 BSW_LPSPI_END(LPSPI_Type* base);

void BSW_LPSPI_WR_BIT(u8 *u8TxData,u8 *u8RxData, u8 u8BuffLen);
void BSW_LPSPI_SEND(u8 *u8TxData, u8 u8BuffLen);


void SPITxSendBuffer(u8 *u8TxData, u8 *u8RxData,u8 u8TxLen) ;




u16 BSW_LPSPI_WR8BIT(LPSPI_Type * base,u16 u16TxData);


uint16_t BSW_READ_FIFO_SIZE(void);




u8 BSW_LPSPI_READ(void);
uint16_t BSW_LPSPI_WR16BIT(LPSPI_Type * base,uint16_t Send_Value); 
void BSW_LPSPI_TxBuffer(LPSPI_Type * base,uint8_t* Send_Buff,uint8_t *Rev_Buff,uint8_t u8_Len);
u8 BSW_LPSPI_SEND_ARRY(u8* P_u8_TxBuff,u8 u8_Lenth);

u8 BSW_LPSPI_Finish_Flg(void);
void BSW_LPSPI_REVE_ARRY(u8* P_u8_RxBuff,u8 u8_Lenth);
void SPIRxSendBuffer(u8 *u8TxData,u8 *u8RxData,u8 u8BuffLen) ;


#endif
